VERSION=v0.1.7
GO_BUILD=go build -ldflags "-s -w -X github.com/TCP404/esdump/constant.VERSION=$(VERSION)" -o

BINARY_NAME=esdump
MAC_AMD_EXE :=./exe/mac-amd/$(BINARY_NAME).dmg
MAC_ARM_EXE :=./exe/mac-arm/$(BINARY_NAME).dmg
LINUX_EXE   :=./exe/linux/$(BINARY_NAME)
WINDOWS_EXE :=./exe/win/$(BINARY_NAME).exe

BUILD_MAC_AMD64     := CGO_ENABLED=0 GOOS=darwin  GOARCH=amd64 $(GO_BUILD) $(MAC_AMD_EXE) main.go
BUILD_MAC_ARM64     := CGO_ENABLED=0 GOOS=darwin  GOARCH=arm64 $(GO_BUILD) $(MAC_ARM_EXE) main.go
BUILD_LINUX         := CGO_ENABLED=0 GOOS=linux   GOARCH=amd64 $(GO_BUILD) $(LINUX_EXE)   main.go
BUILD_WINDOWS       := CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO_BUILD) $(WINDOWS_EXE) main.go

COMPRESS_UPX=upx --best
COMPRESS_MAC_AMD := $(COMPRESS_UPX) $(MAC_AMD_EXE) --force-macos
COMPRESS_LINUX   := $(COMPRESS_UPX) $(LINUX_EXE)
COMPRESS_WINDOWS := $(COMPRESS_UPX) $(WINDOWS_EXE)

start: clean build compress zip

build:
	mkdir -p ./exe
	$(BUILD_MAC_ARM64)
	$(BUILD_MAC_AMD64)
	$(BUILD_LINUX)
	$(BUILD_WINDOWS)

build-win:
	mkdir -p ./exe
	$(BUILD_WINDOWS)

build-linux:
	mkdir -p ./exe
	$(BUILD_LINUX)

build-mac:
	mkdir -p ./exe
	$(BUILD_MAC_ARM64)
	$(BUILD_MAC_AMD64)

compress:
	$(COMPRESS_MAC_AMD)
	$(COMPRESS_LINUX)
	$(COMPRESS_WINDOWS)

compress-win:
	$(COMPRESS_WINDOWS)

compress-linux:
	$(COMPRESS_LINUX)
	
compress-mac:
	$(COMPRESS_MAC_AMD)


.PHONY: clean
clean:
	rm -rf ./exe/*

zip:
	mkdir -p ./exe-$(VERSION)
	zip -r ./exe-$(VERSION)/$(BINARY_NAME)_$(VERSION)_win.zip ./exe/win
	zip -r ./exe-$(VERSION)/$(BINARY_NAME)_$(VERSION)_linux.zip ./exe/linux
	zip -r ./exe-$(VERSION)/$(BINARY_NAME)_$(VERSION)_mac-amd.zip ./exe/mac-amd
	zip -r ./exe-$(VERSION)/$(BINARY_NAME)_$(VERSION)_mac-arm.zip ./exe/mac-arm